package AdminTab;


import java.io.File;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class myKWFramework {
	String[][] xTD, xTC, xTS; //2 dimensional variable to read/Store the test data
	String vKW, vIP1, vIP2;  //Declaring inputs from spreadsheet
	WebDriver myD ;
	String xlPath, xlResPath; //Excel path
	String vError, vResult, vScreenshot, vTempRes;//Declaring the error columns on the excel

	
	@Test
	public void myDriverscript() throws Exception{

	xlPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\Admin\\DevicePrivacyControls\\DevicePrivacyControls.xls";
	xlResPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\Admin\\";
		
	//Read the entire TC and TS
	xTC = xlReadWrite.xlRead(xlPath, "TestCases");
	xTS = xlReadWrite.xlRead(xlPath, "TestSteps");
	xTD = xlReadWrite.xlRead(xlPath, "TestData");
	
	
	for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
		if (xTC[i][3].equalsIgnoreCase("Y")) {	//go to every test step if it is y
			xTC[i][4] = "Pass";  //TC is pass by default on the excel, it updates the excel if the test case/step fails
			for (int j=1; j<xTS.length; j++){ 
			if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
				
				System.out.println("KW to execute is " + xTS[j][4]);
				System.out.println("IP1 to execute is " + xTS[j][5]);
				System.out.println("IP2 to execute is " + xTS[j][6]);
				
				vError = "Pass";
				vResult = "-";
				vScreenshot = "-";
			
					
			try{
				KWexecutor.vKW = xTS[j][4] ;						
				KWexecutor.vIP1 = xTS[j][5];
				KWexecutor.vIP2 = xTS[j][6];
				
			
				vTempRes = KWexecutor.call_KW(); // call the corresponding KW function		
					
			} catch (Exception mye){ //Unexpected error from the Keyword Executor method
				vResult = "Fail";
				vError = "Error is " + mye;
				vScreenshot = "xlResPath" + xTS[j][0] + "-" + xTS[j][1] + ".png";
				File scrFile = ((TakesScreenshot)myD).getScreenshotAs(OutputType.FILE);	//code to capture screenshot			
				FileUtils.copyFile(scrFile, new File(vScreenshot));
				
				System.out.println("Step failed:" + vError);
				xTC[i][4] = "Fail";	
				
				
				
			}
				if(vTempRes.equalsIgnoreCase("Fail")) { //Expected Verify error from Keyword Executor method
				   vResult = "Fail";
				   vError = "Verify Error Occured:" + vKW;
				 //vScreenshot = myD.
				   System.out.println("Step failed:" + vError);
				   xTC[i][4] = "Fail";	
				   vScreenshot = "xlResPath" + xTS[j][0] + "-" + xTS[j][1] + ".png";
				   File scrFile = ((TakesScreenshot)myD).getScreenshotAs(OutputType.FILE);	//code to capture screenshot			
					FileUtils.copyFile(scrFile, new File(vScreenshot));
				}
				
				xTS[j][8] = vResult;
				xTS[j][9] = vError;
				xTS[j][10] = vScreenshot;
				
				}
			}

			}
		//Write back the test Steps with the error
		xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestCase-"+xTC[i][0]+".xls", "TestCase Results", xTC );
		xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestStep-"+xTS[i][0]+".xls", "TestSteps Results", xTS );
		
	
		}
	
	}
}
	




